数据类型
- Dart 语言内建类型:
- Number
- String
- Boolean
- List
- Map
- Set
- Rune
- Symbol
接下来一起来看每种类型的详细用法(配有相关代码示例)。
- Number
int
和 double
都是 num
. 的亚类型。 num 类型包括基本运算 +, -, /, 和 *, 以及 abs()
,ceil()
, 和 floor()
, 等函数方法。 (按位运算符,例如»,定义在 int 类中。) 如果 num 及其亚类型找不到你想要的方法, 尝试查找使用 dart:math 库。
int
整数值不大于64位, 具体取决于平台。 在 Dart VM 上, 值的范围从 -263 到 263 - 1. Dart 被编译为 JavaScript 时,使用 JavaScript numbers, 值的范围从 -253 到 253 - 1.
double
64位(双精度)浮点数,依据 IEEE 754 标准。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34// 定义整数类形
var x = 1;
var hex = 0xD82940;
// 定义 double 类型
var y = 3.14;
var exponents = 1.42e5;
// 从 Dart 2.1 开始,必要的时候 int 字面量会自动转换成 double 类型。
double z = 1; // 相当于 double z = 1.0.
print("------------------- 分割线 ---------------------");
// 数据类型转换
// String -> int
var num = int.parse("12306");
assert(num == 12306);
// String -> double
var dob = double.parse("3.1415926");
assert(dob == 3.1415926);
// int -> String
var int2str = 18.toString();
assert(int2str == "18");
// double -> String
var dob2str = 3.1415.toString();
assert(dob2str == "3.1415");
// 按位操作:移位(<<, >>),按位与(&)以及 按位或(|)
assert((3 << 1) == 6); // 0011 << 1 == 0110
assert((3 >> 1) == 1); // 0011 >> 1 == 0001
assert((3 | 4) == 7); // 0011 | 0100 == 0111String
Dart 字符串是一组 UTF-16 单元序列。 字符串通过单引号或者双引号创建。字符串可以通过
${
expression
}
的方式内嵌表达式。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24// 字符串创建
var s1 = "create s1 string";
var s2 = "create s2 string";
var s3 = "create s3 string";
var s = "Hello Dart, test";
assert("create s1 string $s" == s1 + " Hello Dart, test");
// 字符串拼接
var str = s1 + s2 + s3;
print(str);
// 创建多行字符串
var multipleStr = """
三里清风三里路,
步步清风再无你。
""";
print(multipleStr);
// 创建原始字符串:使用 r 作为前缀
var strs = "http://www.baidu. \ncom";
var originalStr = r"http://www.baidu. \ncom";
print(strs);
print(originalStr);Boolean
Dart 使用
bool
类型表示布尔值。 Dart 只有字面量true
andfalse
是布尔类型, 这两个对象都是编译时常量。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15// 检查空字符串/非空字符串
var emptyStr = "";
assert(emptyStr.isEmpty);
// 判断 0 值
var hintPoint = 0;
assert(hintPoint <= 0);
// 检查 null 值
var unicon;
assert(unicon == null);
// 检查 NaN
var iMeantToDoThis = 0 / 0;
assert(iMeantToDoThis.isNaN);List
Dart 中的 Array 就是 List 对象, 通常称之为 List 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27// 创建列表
var list = [1, 2, 3, 4, 5];
print(list);
// 获取 list 的长度
print(list.length);
// 访问列表的元素
print(list[1] == 2);
// 在 List 字面量之前添加 const 关键字,可以定义 List 类型的编译时常量
var constantList = const [1, 2, 3, true];
// constantList[1] = 1; // 取消注释会引起错误。
// 添加元素
list.insert(5, 6);
list.add(7);
print(list);
// 删除元素
list.removeAt(6);
list.remove(6);
print(list);
// 清除元素
list.clear();
print(list);Map
通常来说, Map 是用来关联 keys 和 values 的对象。 keys 和 values 可以是任何类型的对象。在一个 Map 对象中一个 key 只能出现一次。 但是 value 可以出现多次。 Dart 中 Map 通过 Map 字面量 和 Map 类型来实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38// 创建泛型为<String, String>
var gifts = {
// Key: Value
"first": "Apple",
"second": "ring",
"third": "videos"
};
// 创建泛型为<int, String>
var nodeCase = {0: "apple", 1: "banana", 2: "car", 3: "dot"};
// 添加
var gift = Map(); // 等价于 var gift = new Map();
gift['first'] = 'Apple'; // add key - value
gift['second'] = 'Banana';
gift['third'] = 'Car';
var node = Map();
node[0] = 'apple';
node[1] = 'banana';
node[2] = 'car';
node[3] = 'dot';
assert(gift['first'] == 'Apple');
// 当 Map 中不存在 key 的时候会返回 null
assert(gift['fourth'] == null);
// 获取 Map 的长度
print(gift.length);
// 创建 Map 类型运行时常量,要在 Map 字面量前加上关键字 const。
final constantMap = const {
2: 'helium',
10: 'neon',
18: 'argon',
};
// constantMap[2] = 'Helium'; // ErrorSet
Dart 中 Set 是一个元素唯一且无需的集合。 Dart 为 Set 提供了 Set 字面量和 Set 类型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28// 创建
var createSet = {"one", "two", "three", "four"}; // 自动推断泛型为 String
var nullSet = <String>{}; // 创建空的 Set 对象
// Set<String> names = {}; // 这样也是可以的。
// {} 默认是 Map 类型
// var names = {}; // 这样会创建一个 Map ,而不是 Set 。
print(createSet);
// Set 的相关操作
// 添加
var elements = <String>{};
elements.add("Apple");
elements.addAll(createSet);
print(elements);
// 获取 Set 长度
print(elements.length);
// 创建一个编译时 Set 常量
final constantSet = const {
'Apple',
'Banana',
'Carrot',
'Dot',
'Earth',
};
// constantSet.add('Dot'); // Uncommenting this causes an error.
print(constantSet);Rune(在字符串中标识 Unicode 字符)
在 Dart 中, Rune 用来表示字符串中的 UTF-32 编码字符。Unicode 定义了一个全球的书写系统编码, 系统中使用的所有字母,数字和符号都对应唯一的数值编码。 由于 Dart 字符串是一系列 UTF-16 编码单元, 因此要在字符串中表示 32 位 Unicode 值需要特殊语法支持。表示 Unicode 编码的常用方法是,
\uXXXX
, 这里 XXXX 是一个4位的16进制数。1
2
3
4
5// Rune 格式:\u{xxxx}
var simle = "\u{1f600}";
var heartShape = "\u{2665}";
print(simle + " -- " + heartShape); // 😀 -- ♥Symbol
一个 Symbol 对象表示 Dart 程序中声明的运算符或者标识符。 你也许永远都不需要使用 Symbol ,但要按名称引用标识符的 API 时, Symbol 就非常有用了。 因为代码压缩后会改变标识符的名称,但不会改变标识符的符号。 通过字面量 Symbol ,也就是标识符前面添加一个
#
号,来获取标识符的 Symbol 。1
2
3
4
5var radix = #radix;
var bar = #bar;
print(radix); // Symbol("radix")
print(bar); // Symbol("bar")
至此,Dart 系列之数据类型就结束了,如有任何问题,欢迎交流和指正。